package com.practice.car.cardataapp.interceptor;






import java.net.HttpCookie;
import java.util.Date;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.practice.car.cardataapp.entity.Token;
import com.practice.car.cardataapp.service.SystemManagerService;
import com.practice.car.cardataapp.util.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * car-data-app project
 *
 * @author 陈春幸
 * @date 2020/9/7 0007
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private SystemManagerService ticketService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Cookie[] cookies = request.getCookies();
        String tokenString = null;
        for (int i = 0; i < cookies.length; i++)
        {
            if ("cartoken".equals(cookies[i].getName())) {
                tokenString = cookies[i].getValue();
            }
        }
        if (tokenString == null) {
            return false;
        }
        return TokenUtil.verifyToken(response,tokenString);
    }
}
